//
// Created by Administrator on 2020-2-12.
//

#include "DataStructre.h"
#include "Operate.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int FindIndexOfBookByBookName(bookList * context, char bookName[]){
    for (int i = 0; i < context->len; ++i) {
        if (strcmp(bookName, context->list[i].bookName) == 0) {
            return i;
        }
    }
    return -1;
}

void OutputAllBookByAuthorQuery(bookList * context, char author[]){
    int count;
    for (int i = 0; i < context->len; ++i) {
        if (strcmp(author, context->list[i].author) == 0) {
            count++;
        }
    }
    printf("该作者共找到%d个图书记录", count);
    count = 1;
    for (int i = 0; i < context->len; ++i) {
        if (strcmp(author, context->list[i].author) == 0) {
            printf("\n第%d个记录为：\n", count);                    // Using count again
            OutputById(context, i);
            count++;
        }
    }
}

// Add a new book to arrayList
void Create(bookList * context){
    if (context->len == context->cap){
        grow(context);
    }
    printf("请输入书的登录号：");
    scanf("%d", &context->list[context->len].bookId);
    printf("请输入书名：");
    scanf("%s", context->list[context->len].bookName);
    printf("请输入书的作者：");
    scanf("%s", context->list[context->len].author);
    printf("请输入书的分类号：");
    scanf("%d", &context->list[context->len].category);
    printf("请输入书的出版社：");
    scanf("%s", context->list[context->len].publisher);
    printf("请输入书的出版时间：");
    scanf("%s", context->list[context->len].publicationTime);
    printf("请输入书的价格：");
    scanf("%f", &context->list[context->len].price);
    context->len++;
}

// Get info of book in arrayList
int Retrieve(bookList * context){
    puts("执行查询操作，输入1进行书名查询，2进行作者名查询：");
    printf("请输入查询方式：");
    int mode = GetNum();
    char query[80];
    printf("请输入查询值：");
    scanf("%s", query);
    int id;
    if (mode == 1) {
        id = FindIndexOfBookByBookName(context, query);
        if (id >= 0){
            OutputById(context, id);
            return id;
        } else {
            puts("查询失败，该书不存在");
            return -1;
        }
    } else if (mode == 2){
        OutputAllBookByAuthorQuery(context, query);
    } else {
        printf("请输入正确的查询模式");
        return -2;
    }
}

// Change info of book in arrayList
void Update(bookList * context){
    printf("请先执行查询操作\n");
    int id = Retrieve(context);
    if(id < 0){
        return;
    }
    printf("请输入需要修改的内容：1、登录号 2、书名 3、作者名 4、分类号 5、出版单位 6、出版时间 7、价格 8、取消修改");
    int mode = GetNum();
    if (mode == 1){
        printf("请输入修改后的值：");
        context->list[id].bookId = GetNum();
    } else if (mode == 2){
        printf("请输入修改后的值：");
        scanf("%s", context->list[id].bookName);
    } else if (mode == 3){
        printf("请输入修改后的值：");
        scanf("%s", context->list[id].author);
    } else if (mode == 4){
        printf("请输入修改后的值：");
        context->list[id].category = GetNum();
    } else if (mode == 5){
        printf("请输入修改后的值：");
        scanf("%s", context->list[id].publisher);
    } else if (mode == 6){
        printf("请输入修改后的值：");
        scanf("%s", context->list[id].publicationTime);
    } else if (mode == 7){
        printf("请输入修改后的值：");
        scanf("%f", &context->list[id].price);
    } else {
        return;
    }
    printf("修改后的值为：\n");
    OutputById(context, id);
}

// Delete one book in arrayList. Move the last one to "id" location
void Delete(bookList * context){
    printf("请先执行查询操作\n");
    int id = Retrieve(context);
    if(id < 0){
        return;
    }
    printf("是否删除（y/n）:");
    char op;
    fflush(stdin);
    op = getchar();
    if (op == 'y'){
        Swap(&context->list[id], &context->list[context->len - 1]);
        context->len--;
    } else if (op == 'n'){
        return;
    }
    shrink(context);
}

void OutputAll(bookList *context){
    printf("已储存%d个图书资料\n", context->len);
    for (int i = 0; i < context->len; ++i) {
        printf("第%d个图书信息:\n", i+1);
        printf("该书的登录号为：  %d\n", context->list[i].bookId);
        printf("该书的书名为：    %s\n", context->list[i].bookName);
        printf("该书的作者名为：  %s\n", context->list[i].author);
        printf("该书的分类号为：  %d\n", context->list[i].category);
        printf("该书的出版单位为：%s\n", context->list[i].publisher);
        printf("该书的出版时间为：%s\n", context->list[i].publicationTime);
        printf("该书的价格为：    %f\n", context->list[i].price);
    }
}


// Paginate the number of records, and return the first id of record which will be printed.
static inline int Pagination(bookList * context, int page){
    int startId = (page - 1) * PAGE_SIZE;
    if (startId >= context->len){
        return -1;
    } else {
        return startId;
    }
}

// Paginate and print.
void OutputByPage(bookList * context){
    if (context->len / 5 == 0){
        printf("已按照配置大小把数据分为1页");
    } else {
        printf("已按照配置大小把数据分为%d页", (context->len / PAGE_SIZE) + 1);
    }
    system("pause");
    int page;
    for (;;) {
        system("cls");
        fflush(stdin);
        printf("请输入查看的页数：(-1退出查看)");
        scanf("%d", &page);
        if (page == -1)
            return;

        int startId = Pagination(context, page);
        if (startId == -1){
            printf("输入页数太大，请重试输入...");
            system("pause");
            continue;
        }

        if (startId + PAGE_SIZE < context->len) {
            for (int i = startId; i < startId + PAGE_SIZE; ++i) {
                printf("\n第%d个图书的信息为：\n", i+1);
                OutputById(context, i);
            }
        } else {
            for (int i = startId; i < context->len; ++i) {
                printf("\n第%d个图书的信息为：\n", i+1);
                OutputById(context, i);
            }
        }

        system("pause");
    }
}


static inline void OutputById(bookList * context, int id){
    printf("该书的登录号为：  %d\n", context->list[id].bookId);
    printf("该书的书名为：    %s\n", context->list[id].bookName);
    printf("该书的作者名为：  %s\n", context->list[id].author);
    printf("该书的分类号为：  %d\n", context->list[id].category);
    printf("该书的出版单位为：%s\n", context->list[id].publisher);
    printf("该书的出版时间为：%s\n", context->list[id].publicationTime);
    printf("该书的价格为：    %f\n", context->list[id].price);
    puts("");
}